package com.zaimokuza.cn.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zaimokuza.cn.entity.PageResult;
import com.zaimokuza.cn.entity.blog.Blog;
import com.zaimokuza.cn.entity.blog.Tag;
import com.zaimokuza.cn.entity.constants.ActionConstant;
import com.zaimokuza.cn.entity.enums.CustomizeExceptionEnum;
import com.zaimokuza.cn.mapper.BlogMapper;
import com.zaimokuza.cn.mapper.BlogSpreadMapper;
import com.zaimokuza.cn.service.*;
import com.zaimokuza.cn.util.AssertUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;

/**
 * @author zaimokuza
 */
@Service
public class BlogServiceImpl implements BlogService {

    @Resource
    private ActionService actionService;
    @Resource
    private CommentService commentService;
    @Resource
    private LikeService likeService;
    @Resource
    private UserService userService;
    @Resource
    private TagService tagService;
    @Resource
    private BlogMapper blogMapper;
    @Resource
    private BlogSpreadMapper blogSpreadMapper;

    @Override
    public void addBlog(Blog blog) {
        // 博客ID，作者ID
        String blogId = blog.getId();
        String authorId = blog.getAuthorId();
        // 添加记录
        blogMapper.insertBlog(blog);
        blogSpreadMapper.insertBlogSpread(blogId);
        // 增加博客总数
        userService.increaseBlogTotal(authorId);
        // 添加标签信息
        tagService.addBlogTagRelation(blogId, blog.getTagIdList());
        // 添加动态
        actionService.addAction(ActionConstant.TYPE_BLOG, authorId, blogId, null);
    }

    @Override
    public void updateBlog(Blog blog, String userId) {
        // 博客ID
        String blogId = blog.getId();
        // 校验用户是否有权限操作博客
        Integer isBlogAuthor = blogMapper.queryIsBlogAuthor(blogId, userId);
        AssertUtils.notNull(isBlogAuthor, CustomizeExceptionEnum.BLOG_NOT_FOUND);
        // 更新博客标签关联信息
        tagService.updateBlogTagRelation(blogId, blog.getTagIdList());
        // 更新博客信息
        blogMapper.updateBlog(blog);
    }

    @Override
    public void deleteBlogForUser(String blogId, String userId) {
        // 校验用户是否有权限操作博客
        Integer isBlogAuthor = blogMapper.queryIsBlogAuthor(blogId, userId);
        AssertUtils.notNull(isBlogAuthor, CustomizeExceptionEnum.BLOG_NOT_FOUND);
        // 博客属于当前用户时进行删除操作
        deleteBlog(blogId);
    }

    @Override
    public void deleteBlog(String id) {
        // 校验博客是否存在
        Integer blogIsExist = blogMapper.queryBlogIsExist(id);
        AssertUtils.notNull(blogIsExist, CustomizeExceptionEnum.BLOG_NOT_EXIST);
        // 逻辑删除博客信息
        blogMapper.updateBlogStatus(id);
        // 删除博客扩展信息
        blogSpreadMapper.deleteBlogSpread(id);
        // 减少博客作者的博客总数
        String authorId = blogMapper.queryBlogAuthorIdById(id);
        userService.reduceBlogTotal(authorId);
        // 删除博客标签关联信息
        tagService.deleteBatchRelationByBlogId(id);
        // 删除博客相关的评论
        commentService.deleteCommentByBlog(id);
        // 删除点赞记录
        likeService.deleteBlogLikeAndCommentLikeByBlogId(id);
        // 删除动态
        actionService.deleteAction(ActionConstant.TYPE_BLOG, id, null);
    }

    @Override
    public PageResult<Blog> getBlogPage(Blog param, Integer page, Integer size) {
        // 分页
        PageHelper.startPage(page, size);
        // 查询
        Page<Blog> blogPage = blogMapper.queryBlogPage(param);
        // 封装结果并返回
        return PageResult.from(blogPage);
    }

    @Override
    public Blog getBlogDetail(String blogId, String userId) {
        // 查询博客基础信息
        Blog blog = blogMapper.queryBlog(blogId, userId);
        // 校验博客是否存在
        AssertUtils.notNull(blog, CustomizeExceptionEnum.BLOG_NOT_FOUND);
        // 查询博客标签关联信息
        ArrayList<Tag> tagList = tagService.getTagListByBlogId(blogId);
        blog.setTagList(tagList);
        // 补充信息
        blog.setIsAuthor(userId != null && userId.equals(blog.getAuthorId()));
        // 返回
        return blog;
    }

    @Override
    public Blog getBlogDetailForUpdate(String blogId, String userId) {
        // 校验用户是否有权限操作博客
        Integer isBlogAuthor = blogMapper.queryIsBlogAuthor(blogId, userId);
        AssertUtils.notNull(isBlogAuthor, CustomizeExceptionEnum.BLOG_NOT_FOUND);
        // 查询博客信息
        Blog blog = blogMapper.queryBlog(blogId, null);
        ArrayList<String> tagIdList = tagService.getTagIdListByBlogId(blogId);
        blog.setTagIdList(tagIdList);
        // 返回结果
        return blog;
    }

    @Override
    public ArrayList<Blog> getRecommendBlogList(String time) {
        return blogMapper.queryRecommendBlogList(time);
    }

    @Override
    public ArrayList<Blog> getSearchBlogList(String str, String time) {
        return blogMapper.querySearchBlogList(str, time);
    }

    /* 统计相关操作 */

    @Override
    public void increaseBlogLikes(String blogId) {
        blogSpreadMapper.increaseBlogLikes(blogId);
    }

    @Override
    public void reduceBlogLikes(String blogId) {
        blogSpreadMapper.reduceBlogLikes(blogId);
    }

    @Override
    public void increaseBlogPageViews(String blogId) {
        blogSpreadMapper.increaseBlogPageViews(blogId);
    }

}
